package com.musicxapp.modules.mywebview;


import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.webkit.CookieManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Toast;

import com.musicxapp.MainActivity;
import com.musicxapp.R;

public class WebActivity extends Activity {
    private WebView mWebView;
    private WebActivity self;
    private String url ="https://y.qq.com";//带cookie的页面url
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview_layout);

        self = WebActivity.this;
        mWebView = (WebView) findViewById(R.id.webview_forshow);
        mWebView.getSettings().setAppCacheEnabled(false);
        mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        mWebView.getSettings().setDatabaseEnabled(false);
        mWebView.getSettings().setDomStorageEnabled(false);
        mWebView.getSettings().setGeolocationEnabled(false);
//        mWebView.getSettings().setPluginsEnabled(false);
        mWebView.getSettings().setSaveFormData(false);
        mWebView.getSettings().setSavePassword(false);
        mWebView.getSettings().setJavaScriptEnabled(true); ///------- 设置javascript 可用
        mWebView.getSettings().setUserAgentString("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36");
        mWebView.loadUrl(url);
        mWebView.setWebChromeClient(new WebChromeClient()
        {
            @Override
            public void onProgressChanged(WebView view, int newProgress)
            {
                super.onProgressChanged(view, newProgress);
                view.requestFocus();
            }
        });
        mWebView.setWebViewClient(new MyWebViewClient(self));

    }

    @Override
    protected void onStop() {
        CookieManager cookieManager = CookieManager.getInstance();
        String CookieStr = cookieManager.getCookie(url);
        if(CookieStr.contains("psrf_qqaccess_token")){
            Toast.makeText(this,CookieStr,Toast.LENGTH_SHORT).show();
            SharedPreferences qq_cookie = getSharedPreferences("qq_cookie", Context.MODE_PRIVATE);
            SharedPreferences.Editor edit = qq_cookie.edit();
            edit.putString("cookie",CookieStr);
            edit.commit();
        }
        super.onStop();
    }
}
